package com.welink.cmp.gatewayimpl.metadata;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.common.OperationLogService;
import com.welink.cmp.dao.metadata.HostDaoService;
import com.welink.cmp.dao.metadata.dataobject.HostDO;
import com.welink.cmp.dao.metadata.dataobject.SwitchDO;
import com.welink.cmp.domain.metadata.Host;
import com.welink.cmp.domain.metadata.Switch;
import com.welink.cmp.domain.metadata.enums.ResourceTypeEnum;
import com.welink.cmp.domain.metadata.gateway.IHostGateway;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yanwei li create  2024-06-18 14:31
 */
@Service
@Primary
@Slf4j
public class HostGatewayImpl implements IHostGateway {


    @Autowired
    private HostDaoService hostDaoService;

    @Autowired
    private OperationLogService logService;

    /**
     * @param host
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Host saveOrUpdate(Host host) {
        HostDO hostDO = GenericConvertor.toDO(host, HostDO.class);
        final boolean res = hostDaoService.saveOrUpdate(hostDO);
        logService.saveOperationLog(ResourceTypeEnum.HOST.getType(), hostDO.getId(),host.getId()==null?"新增":"更新","admin","操作结果：" +res);
        BeanUtils.copyProperties(hostDO,host);

        // 发送 Host 变更 MQ
        return host;
    }

    /**
     * @return
     */
    @Override
    public Boolean hostStatusChange() {


        return null;
    }

    /**
     * 通过 SN 号批量保存或者更新主机
     *
     * @param hostList
     */
    @Override
    @Transactional
    public Boolean saveOrUpdateBatchBySn(List<Host> hostList) {

        //过滤全部 SN
        final List<String> allSnList = hostList.stream().map(Host::getSn).collect(Collectors.toList());
        LambdaQueryWrapper<HostDO> queryWrapper = Wrappers.lambdaQuery(HostDO.class);
        queryWrapper.in(HostDO::getSn,allSnList);
        final List<HostDO> existHostList = hostDaoService.list(queryWrapper);
        // 已存在的记录 SN-ID
        final Map<String, Long> existHostMap = existHostList.stream().collect(Collectors.toMap(HostDO::getSn, HostDO::getId));
        hostList.forEach(hostDo -> {
            final Long swId = existHostMap.get(hostDo.getSn());
            if ( swId !=null){
                hostDo.setId(swId);
            }
        });
        List<HostDO> allHostList = GenericConvertor.toDOList(hostList, HostDO.class);
        // 批量更新
        final boolean rst = hostDaoService.saveOrUpdateBatch(allHostList);
        log.info("根据 Sn 批量更新或插入主机 Host [{}]，更新Sn记录{},全部Sn{} ",rst,existHostList.size(),hostList.size());
        return rst;
    }

    /**
     * Query
     *
     * @param name
     ***/
    @Override
    public List<Host> getHostListByName(List<String> name) {
        LambdaQueryWrapper<HostDO> wrapper = Wrappers.lambdaQuery(HostDO.class);
        wrapper.in(HostDO::getName,name);
        List<HostDO> list = hostDaoService.list(wrapper);
        List<Host> hostList = GenericConvertor.toCOList(list, Host.class);
        return hostList;
    }

    /**
     * @param sn
     * @return
     */
    @Override
    public List<Host> getHostListBySn(List<String> sn) {
        LambdaQueryWrapper<HostDO> wrapper = Wrappers.lambdaQuery(HostDO.class);
        wrapper.in(HostDO::getSn,sn);
        List<HostDO> list = hostDaoService.list(wrapper);
        List<Host> hostList = GenericConvertor.toCOList(list, Host.class);
        return hostList;
    }

    /**
     * @param assetsId
     * @return
     */
    @Override
    public List<Host> getHostIdListByAssetsId(List<String> assetsId) {
        LambdaQueryWrapper<HostDO> wrapper = Wrappers.lambdaQuery(HostDO.class);
        wrapper.in(HostDO::getAssetsId,assetsId);
        List<HostDO> list = hostDaoService.list(wrapper);
        List<Host> hostList = GenericConvertor.toCOList(list, Host.class);
        return hostList;
    }
}
